import torch.nn as nn
import torch.nn.functional as F


class FPNHead(nn.Module):
    def __init__(self, in_channels=512, out_channels=1):
        super().__init__()
        # 上采样解码器
        self.conv1 = nn.Conv2d(in_channels, 256, kernel_size=3, padding=1)
        self.bn1 = nn.BatchNorm2d(256)
        self.conv2 = nn.Conv2d(256, 128, kernel_size=3, padding=1)
        self.bn2 = nn.BatchNorm2d(128)
        self.conv3 = nn.Conv2d(128, out_channels, kernel_size=1)

    def forward(self, x):
        # 上采样和卷积
        x = F.relu(self.bn1(self.conv1(x)))
        x = F.interpolate(x, scale_factor=2, mode='bilinear', align_corners=True)
        x = F.relu(self.bn2(self.conv2(x)))
        x = F.interpolate(x, scale_factor=2, mode='bilinear', align_corners=True)
        x = self.conv3(x)

        # 上采样到原始尺寸
        return F.interpolate(x, scale_factor=8, mode='bilinear', align_corners=True)